<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
      <el-form-item label="姓名" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="身份证" prop="identityCard">
        <el-input
          v-model="queryParams.identityCard"
          placeholder="请输入身份证"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="省/市/区" prop="region">
            <cascader
              v-model="queryParams.region"
              @provincesValueChang="provincesValueChang"
              style="width: 100%"
            ></cascader>
          </el-form-item>
      <el-form-item label="是否死亡" prop="dieFlag">
        <el-select v-model="queryParams.dieFlag" placeholder="请选择" clearable>
          <el-option label="全部" value=""></el-option>
          <el-option label="是" :value='1'></el-option>
          <el-option label="否" :value='0'></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="是否五保户" prop="fiveGuaranteedFlag">
        <el-select v-model="queryParams.fiveGuaranteedFlag" placeholder="请选择" clearable>
          <el-option label="全部" value=""></el-option>
          <el-option label="是" :value='1'></el-option>
          <el-option label="否" :value='0'></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleImport"
        >导入</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table
      v-if="refreshTable"
      v-loading="loading"
      :data="tableData"
      row-key="menuId"
    >
      <el-table-column prop="name" label="姓名" :show-overflow-tooltip="true" minWidth="160"></el-table-column>
      <el-table-column prop="age" label="年龄" :show-overflow-tooltip="true" minWidth="160"></el-table-column>
      <el-table-column prop="genderStr" label="性别" :show-overflow-tooltip="true" minWidth="160"></el-table-column>
      <el-table-column prop="identityCard" label="身份证信息" :show-overflow-tooltip="true" minWidth="160"></el-table-column>
      <el-table-column prop="province" label="省" :show-overflow-tooltip="true" minWidth="160"></el-table-column>
      <el-table-column prop="city" label="市" :show-overflow-tooltip="true" minWidth="160"></el-table-column>
      <el-table-column prop="area" label="区" :show-overflow-tooltip="true" minWidth="160"></el-table-column>
      <el-table-column prop="address" label="详细住址" :show-overflow-tooltip="true" minWidth="160"></el-table-column>
      <el-table-column prop="fiveGuaranteedFlag" label="是否五保户" :show-overflow-tooltip="true" minWidth="160">
        <template slot-scope="scope">
          <span>{{ scope.row.fiveGuaranteedFlag | formatFlag }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="historicalDisease" label="历史疾病" :show-overflow-tooltip="true" minWidth="160"></el-table-column>
      <el-table-column prop="solitudeFlag" label="是否独居" :show-overflow-tooltip="true" minWidth="160">
        <template slot-scope="scope">
          <span>{{ scope.row.solitudeFlag | formatFlag }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="familyCase" label="家庭情况" :show-overflow-tooltip="true" minWidth="160"></el-table-column>
      <el-table-column prop="dieFlag" label="是否死亡" :show-overflow-tooltip="true" minWidth="160">
        <template slot-scope="scope">
          <span>{{ scope.row.dieFlag | formatFlag }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="remark" label="备注" :show-overflow-tooltip="true" minWidth="160"></el-table-column>
      <el-table-column label="操作" align="center" fixed="right" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:menu:edit']"
          >修改</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.current"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- 添加或修改菜单对话框 -->
    <AddModal
      v-model="addVisible"
      :detail="row"
      @refresh="handleQuery"
    />
    <ImportModal v-model="importVisible" />
    <el-dialog :title="upload.title" :visible.sync="upload.open" width="400px" append-to-body>
      <el-upload ref="upload" :limit="1" accept=".xlsx, .xls" :headers="upload.headers" :action="upload.url"
      :disabled="upload.isUploading" :on-progress="handleFileUploadProgress" :on-success="handleFileSuccess" :auto-upload="false" drag>
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <div class="el-upload__tip text-center" slot="tip">
          <span>仅允许导入xls、xlsx格式文件。</span>
          <el-link type="primary" :underline="false" style="font-size: 12px; vertical-align: baseline" @click="importTemplate">下载模板</el-link>
        </div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import cascader from "@/components/cascader/index.vue";
import { add, queryPage, get, update } from "@/api/pension/elderlyInfo";
import AddModal from "./components/add-modal.vue";
import ImportModal from "./components/import-modal.vue";
import { getToken } from "@/utils/auth";

export default {
  name: "OldPerson",
  components: { AddModal, ImportModal ,cascader},
  filters: {
    formatFlag(val) {
      return val ? '是' : '否'
    }
  },
  data() {
    return {
      // 用户导入参数
      upload: {
        // 是否显示弹出层（用户导入）
        open: false,
        // 弹出层标题（用户导入）
        title: "",
        // 是否禁用上传
        isUploading: false,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/pension/elderlyInfo/importData"
      },
      // 遮罩层
      loading: false,
      // 显示搜索条件
      showSearch: true,
      // 重新渲染表格状态
      refreshTable: true,
      // 查询参数
      queryParams: {
        name: undefined,
        idCard: undefined,
        dieFlag: '',
        fiveGuaranteedFlag: ''
      },
      total: 0,
      addVisible: false,
      importVisible: false,
      row: null,
      tableData: []
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 导入按钮操作 */
    handleImport() {
      this.upload.title = "用户导入";
      this.upload.open = true;
    },
    /** 下载模板操作 */
    importTemplate() {
      this.download('pension/elderlyInfo/importTemplate', {
      }, `elderlyInfoTemplate_${new Date().getTime()}.xlsx`)
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false;
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();
      // this.$alert("<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" + response.msg + "</div>", "导入结果", { dangerouslyUseHTMLString: true });
      this.getList();
    },
    // 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit();
    },
    // 省市区
    provincesValueChang(value) {
      this.queryParams.province = value[0];
      this.queryParams.city = value[1];
      this.queryParams.area = value[2];
    },
    /** 查询菜单列表 */
    getList() {
      this.loading = true;
      queryPage(this.queryParams).then(response => {
        this.tableData = response.data.list;
        this.tableData.forEach(item=>{
          item.region = [item.province,item.city,item.area]
          item.fiveGuaranteedFlag = item.fiveGuaranteedFlag ? 1 : 0;
          item.dieFlag = item.dieFlag ? 1 : 0;
          item.solitudeFlag = item.solitudeFlag ? 1 : 0;
          item.genderStr = item.gender == '01'?'男':'女';
        })
        this.total = response.data.totalCount;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams.province =null;
      this.queryParams.city = null;
      this.queryParams.area = null;
      this.resetForm("queryForm");
      this.handleQuery();
    },
    handleAdd() {
      this.row = null
      this.addVisible = true
    },
    // handleImport() {
    //   this.importVisible = true
    // },
    handleUpdate(row) {
      this.row = row
      this.addVisible = true
    }
  }
};
</script>
